﻿using System;
using System.Drawing;
using System.Windows.Forms;
using com.magickms.api.Gdi32;
using com.magickms.api.Struct;
using com.magickms.api.User32;

namespace com.magickms.util
{
    public class Bean
    {
        /// <summary>
        /// 屏幕截图
        /// </summary>
        /// <returns></returns>
        public static Image CaptureScreen()
        {
            return CaptureImage(User32API.GetDesktopWindow());
        }

        /// <summary>
        /// 窗口截图
        /// </summary>
        /// <returns></returns>
        public static Image CatpureWindow()
        {
            return CaptureImage(User32API.GetForegroundWindow());
        }

        /// <summary>
        /// 窗口靠边
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="top">是否向上靠齐：true向上靠齐，false向下靠齐</param>
        /// <param name="left">是否向左靠齐：true向左靠齐，false向右靠齐</param>
        public static void CornerWindow(IntPtr handle, bool top, bool left)
        {
            if (handle == IntPtr.Zero)
            {
                return;
            }

            Rectangle size = Screen.PrimaryScreen.WorkingArea;
            var rect = new RECT();
            User32API.GetWindowRect(handle, ref rect);
            int w = rect.right - rect.left;
            int h = rect.bottom - rect.top;
            int x = left ? 0 : size.Width - w;
            int y = top ? 0 : size.Height - h;
            User32API.MoveWindow(handle, x, y, w, h, true);
        }

        /// <summary>
        /// 窗口居中
        /// </summary>
        /// <param name="handle"></param>
        public static void CenterWindow(IntPtr handle)
        {
            if (handle == IntPtr.Zero)
            {
                return;
            }

            Rectangle size = Screen.PrimaryScreen.WorkingArea;
            var rect = new RECT();
            User32API.GetWindowRect(handle, ref rect);
            int w = rect.right - rect.left;
            int h = rect.bottom - rect.top;
            int x = (size.Width - w) >> 1;
            int y = (size.Height - h) >> 1;
            User32API.MoveWindow(handle, x, y, w, h, true);
        }

        public static void MoveWindow(IntPtr handle, int x, int y)
        {
            if (handle == IntPtr.Zero)
            {
                return;
            }

            var rect = new RECT();
            User32API.GetWindowRect(handle, ref rect);
            int w = rect.right - rect.left;
            int h = rect.bottom - rect.top;
            User32API.MoveWindow(handle, x, y, w, h, true);
        }

        /// <summary>
        /// 调整窗口大小
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="w">目标宽度</param>
        /// <param name="h">目标高度</param>
        /// <param name="scale">是否缩放窗口</param>
        /// <param name="ratio">是否维持比例</param>
        public static void ResizeWindow(IntPtr handle, int w, int h, bool scale, bool ratio)
        {
            if (handle == IntPtr.Zero)
            {
                return;
            }

            var rect = new RECT();
            User32API.GetWindowRect(handle, ref rect);
            int fw = rect.right - rect.left;
            int fh = rect.bottom - rect.top;
            //缩放窗口
            if (scale)
            {
                // 维持比例
                if (ratio)
                {
                    double rw = w / (double)fw;
                    double rh = h / (double)fh;
                    double r = rw < rh ? rw : rh;
                    fw = (int)(fw * r);
                    fh = (int)(fh * r);
                }
                else
                {
                    fw = w;
                    fh = h;
                }
            }

            int fx = rect.left;
            int fy = rect.top;
            User32API.MoveWindow(handle, fx, fy, fw, fh, true);
        }

        /// <summary>
        /// .Net抓取
        /// </summary>
        /// <param name="point"></param>
        /// <param name="rect"></param>
        public static Image CaptureImage(Point point, Rectangle rect)
        {
            var bitmap = new Bitmap(rect.Width, rect.Height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                // g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);
                g.CopyFromScreen(point.X, point.Y, (bitmap.Width - rect.Width) >> 1, (bitmap.Height - rect.Height) >> 1, rect.Size);
            }
            return bitmap;
        }

        /// <summary>
        /// GDI抓取
        /// </summary>
        /// <param name="handle"></param>
        /// <returns></returns>
        public static Image CaptureImage(IntPtr handle)
        {
            IntPtr hdcSrc = User32API.GetWindowDC(handle);
            var winRect = new RECT();
            User32API.GetWindowRect(handle, ref winRect);
            int width = winRect.right - winRect.left;
            int height = winRect.bottom - winRect.top;
            IntPtr hdcDest = Gdi32API.CreateCompatibleDC(hdcSrc);
            IntPtr hBitmap = Gdi32API.CreateCompatibleBitmap(hdcSrc, width, height);
            IntPtr hOld = Gdi32API.SelectObject(hdcDest, hBitmap);
            Gdi32API.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Gdi32API.SRCCOPY);
            Gdi32API.SelectObject(hdcDest, hOld);
            Gdi32API.DeleteDC(hdcDest);
            User32API.ReleaseDC(handle, hdcSrc);
            Image img = Image.FromHbitmap(hBitmap);
            Gdi32API.DeleteObject(hBitmap);
            return img;
        }
    }
}
